<template>
  <div class="base-container">
    <t-form ref="form" :data="queryParams" label-width="80"
      @submit="() => { this.pagination.current = 1; this.queryParams.pageNum = 1; this.getList() }">
      <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
        <t-col :span="11" class="query-form-inline">
          <t-form-item label="业务名称" name="businessName">
            <t-input v-model="queryParams.businessName" clearable placeholder="请输入项目名称" style="width: 240px" />
          </t-form-item>
          <t-form-item v-if="!hideApproveType" label="业务类型" name="approveType">
            <t-select style="width: 240px" clearable v-model="queryParams.approveType"
              :options="dict.type.approve_type">
            </t-select>
          </t-form-item>
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit"> 查询</t-button>
          <t-button theme="default" variant="outline" type="reset">重置</t-button>
        </t-col>
      </t-row>
      <t-row style="margin-bottom: 10px">
        <t-col :span="12" class="">
          <t-button @click="exportExcelOther" variant="outline" theme="primary">
            <t-icon name="download" slot="icon" />
            导出我已审批项目
          </t-button>
        </t-col>
      </t-row>
    </t-form>
    <t-table rowKey="id" :data="data"
      :columns="!hideInfo ? columns : columns.filter(val => { return val.title !== '填报单位' })" hover
      :pagination="pagination" :loading="dataLoading" :sort="sort" :hideSortTips="true" @sort-change="sortChange">
      <template #[`project.projectName`]="{ row }">

        <t-link @click="openProjectDetail(row)" hover="color" theme="primary">{{ row.project.projectName }}</t-link>
      </template>
      <template #[`project.projectStatus`]="{ row }">
        <t-tag v-if="hideInfo" class="cursor-pointer"
          :theme="handleTheme(row.project.projectStatus, dict.type.project_status)" variant="light">
          {{ row.project.projectStatus | filterByDict(dict.type.project_status) }}
        </t-tag>
        <t-tag v-else
          @click="dialog.header = row.project.projectName; dialog.objId = row.project.id; dialog.open_approve_list = true"
          class="cursor-pointer" :theme="handleTheme(row.project.projectStatus, dict.type.project_status)"
          variant="light">
          {{ row.project.projectStatus | filterByDict(dict.type.project_status) }}
        </t-tag>
      </template>
      <template #approveType="{ row }">
        {{ row.approveType | filterByDict(dict.type.approve_type) }}
      </template>
      <template #approveStatus="{ row }">
        <t-tag v-if="row.approveStatus" :theme="handleTheme(row.approveStatus, dict.type.approve_status)"
          variant="light">
          {{ row.approveStatus | filterByDict(dict.type.approve_status) }}
        </t-tag>
        <t-tag v-else theme="warning" variant="light">待审批
        </t-tag>
        <!--        {{ row.approveStatus|filterByDict(dict.type.approve_status) }}-->
      </template>
      <template #op="{ row }">
        <t-button v-if="!reviewOpen" @click="dialog.row = row; dialog.open = true" size="small" variant="text"
          class="t-button-link">
          <t-icon name="info-circle" slot="icon" />
          审批详情
        </t-button>
        <t-button v-else @click="dialog.row = row; dialog.open = true" size="small" variant="text"
          class="t-button-link">
          <t-icon name="edit-1" slot="icon" />
          继续审批
        </t-button>
      </template>
    </t-table>
    <project-detail-dialog :row="dialog.row" :tabDefaultValue="dialog.tabDefaultValue" :hideInfo="hideInfo"
      :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_project_detail"
      v-if="dialog.open_project_detail"></project-detail-dialog>
    <item-detail-dialog :tabDefaultValue="dialog.tabDefaultValue" :hideInfo="hideInfo" :header="dialog.header"
      :objId="dialog.objId" :visible.sync="dialog.open_item_detail" v-if="dialog.open_item_detail"
      :row="dialog.row"></item-detail-dialog>
    <outline-detail-dialog :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_outline_detail"
      v-if="dialog.open_outline_detail"></outline-detail-dialog>
    <contract-detail-dialog :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_contract_detail"
      v-if="dialog.open_contract_detail"></contract-detail-dialog>
    <contract-new-detail-dialog :header="dialog.header" :objId="dialog.objId"
      :visible.sync="dialog.open_new_contract_detail"
      v-if="dialog.open_new_contract_detail"></contract-new-detail-dialog>
    <approve-list :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_approve_list"
      v-if="dialog.open_approve_list"></approve-list>
    <approve-info :visible.sync="dialog.open" @reload="getList()" :row="dialog.row" :reviewOpen="reviewOpen"
      v-if="dialog.open">
    </approve-info>
  </div>
</template>

<script>
import OutlineDetailDialog from "../outline/components/outline-detail-dialog.vue";
import ContractDetailDialog from "../contract/components/contract-detail-dialog.vue";
import ContractNewDetailDialog from "../contract/components/contract-new-detail-dialog.vue";
import ApproveList from "./components/approve-list.vue";
import ProjectDetailDialog from "../pm/components/project-detail-dialog.vue";
import ItemDetailDialog from "../pm/components/item-detail-dialog.vue";
import ApproveInfo from "./components/approve-info.vue"


export default {
  name: "index",
  dicts: ["approve_type", "approve_status", "project_status"],
  components: {
    OutlineDetailDialog,
    ContractDetailDialog,
    ApproveList,
    ProjectDetailDialog,
    ApproveInfo,
    ItemDetailDialog,
    ContractNewDetailDialog
  },
  data () {
    return {
      sort: [],
      hideApproveType: false,
      hideInfo: this.$store.state.user.roles.includes("xssc"),
      site_tag: import.meta.env.VITE_SITE_TAG,
      reviewOpen: false,
      dialog: {
        open: false,
        header: "",
        row: {},
        data: {},
        open_project_detail: false,
        open_outline_detail: false,
        open_contract_detail: false,
        open_new_contract_detail: false,
        objId: 0,
        open_approve_list: false,
        tabDefaultValue: '1',
        open_item_detail: false,
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        approveType: '',
        businessName: '',
      },
      data: [],
      columns: [
        {
          colKey: "sort",
          title: "项目编号",
          align: "center",
          width: 120,
          // sorter: true,
        },
        {
          colKey: "project.projectName",
          title: "项目名称",
          align: "center",
          // ellipsis: true,
          width: 220,
          // sorter: true,
        },
        {
          colKey: "project.projectStatus",
          title: "项目状态",
          align: "center",
          // ellipsis: true,
          width: 220,
        },
        // {
        //   colKey: "businessId",
        //   title: "审批内容",
        //   align: "center",
        //   ellipsis: true
        // },
        // {
        //   colKey: "businessName",
        //   title: "业务名称",
        //   align: "center",
        //   ellipsis: true
        // },
        {
          colKey: "approveType",
          title: "审批业务类型",
          align: "center",
          width: 120
        },
        {
          colKey: "opinion",
          title: "审批意见",
          align: "center",
          width: 300
        },
        {
          colKey: "approveStatus",
          title: "审批结果",
          align: "center",
        },
        {
          colKey: "project.declareDeptName",
          title: "填报单位",
          align: "center",
          width: 180,
          // sorter: true,
        },
        {
          colKey: "updateTime",
          title: "操作时间",
          align: "center",
          width: 180
        },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 200
        },
      ],

      dataLoading: false,
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10, 20, 30, 50],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination, pageInfo)
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList()
        },
      },
    }
  },
  async created () {
    if (this.$route.meta.query) {
      Object.assign(this.queryParams, JSON.parse(this.$route.meta.query))
      // 固定查询不可选
      if (this.queryParams.approveType) {
        this.hideApproveType = true;
      }
    }

    // 集团形式审查人员开启审批后再编辑
    // if (this.hideInfo && this.site_tag === 'jt') {
    //   this.reviewOpen = true;
    // }

    this.getList()
  },
  methods: {
    exportExcelOther () {
      this.$download(`kjxm/excel/approvedList `, this.queryParams, `我已审批列表_${new Date().getTime()}.xls`)
    },
    sortChange (sort) {
      this.sort = sort;
      let orderByColumn = []
      let isAsc = []
      if (sort instanceof Array) {
        orderByColumn = sort.map(val => val.sortBy)
        isAsc = sort.map(val => val.descending ? `desc` : `asc`)
      } else {
        orderByColumn = [sort.sortBy]
        isAsc = [sort.descending ? `desc` : `asc`]
      }
      this.queryParams.orderByColumn = orderByColumn.join(",")
      this.queryParams.isAsc = isAsc.join(",")
      this.getList();
    },
    getList () {
      this.dataLoading = true;
      this.$api.kjxm.approve.myApprovedList(this.queryParams).then(response => {
        this.pagination.total = response.total;
        this.data = response.rows;
        this.dataLoading = false;
      }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },
    openProjectDetail (row) {
      this.dialog.objId = row.project.id;
      this.dialog.header = row.project.projectName;
      this.dialog.row = row
      if (row.approveType === '1') {
        this.dialog.tabDefaultValue = '1';
      } else if (row.approveType === '2') {
        this.dialog.tabDefaultValue = '2';
      } else if (row.approveType === '3') {
        this.dialog.tabDefaultValue = '3';
      } else if (row.approveType === '6') {
        this.dialog.tabDefaultValue = '4';
      }
      if (row.project.version === '2024') {
        this.dialog.open_item_detail = true;
      } else {
        this.dialog.open_project_detail = true;
      }
    }
  },
}
</script>

<style scoped></style>
